using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace GarbageTruck.MonthOfTheYear
{
    public class MonthOfTheYear : ITemporalExpression
    {

        #region CONSTRUCTORS

        public MonthOfTheYear()
        {
        }

        public MonthOfTheYear(string expresion)
        {
            this.Number = int.Parse(expresion);
        }

        public MonthOfTheYear(int number)
        {
            this.Number = number;
        }

        #endregion CONSTRUCTORS

        #region FIELDS

        private int m_Number;
        private Calendar calendar = CultureInfo.CurrentCulture.Calendar;

        #endregion FIELDS

        #region PROPERTIES

        public int Number
        {
            get { return m_Number; }
            set { m_Number = value; }
        }

        #endregion PROPERTIES

        #region METHODS

        public bool Contains(DateTime dateTime)
        {
            if (Number > 0)
            {
                return (dateTime.Month == this.Number);
            }
            else
            {
                return (dateTime.Month == (13 - this.Number));
            }
        }

        public DateTime? GetNextOcurrence(DateTime dateTime)
        {
            DateTime currentYearOcurrence = new DateTime(dateTime.Year, this.Number, 1);
            DateTime nextYearOcurrence = new DateTime(dateTime.Year + 1, this.Number, 1);

            if (dateTime.Date < currentYearOcurrence)
            {
                return currentYearOcurrence;
            }
            else if (dateTime.Date > currentYearOcurrence && Contains(dateTime.Date.AddDays(1)))
            {
                return dateTime.Date.AddDays(1);
            }
            else
            {
                return nextYearOcurrence;
            }
        }

        public DateTime? GetEventEnd(DateTime dateTime)
        {
            return dateTime;
        }

        public override string ToString()
        {
            return string.Format("MOTY({1})", this.Number);
        }

        #endregion METHODS

    }
}
